package TwentyOne.Class.Map;

/*
 多对多映射
 用户信息：用户ID、姓名
 商品：商品ID、商品名称、商品价格
 访问记录：用户ID、商品ID

 获取一个用户访问的所有商品的信息；
 获取一个商品被浏览过的全部的用户信息。
 */





class Member {

    private String mid;
    private String name;
    private Product products[];

    public Member(String mid, String name) {
        this.mid = mid;
        this.name = name;
    }

    public void setProducts(Product products[]) {
        this.products = products;
    }

    public Product[] getProducts() {
        return this.products;
    }
    public String getInfo(){
        return "【用户信息】mid = "+this.mid+"、name = "+this.name;
    }
}

class Product{
    private long pid;
    private String title;
    private double price;
    private Member members[];
    public Product(long pid,String title,double price){
        this.pid=pid;
        this.title=title;
        this.price=price;
    }
    public void setMembers(Member members[]){
        this.members=members;
    }
    public Member[] getMembers(){
        return this.members;
    }
    public String getInfo(){
        return "【商品信息】pid = "+this.pid+"、title = "+this.title+"、price = "+this.price;
    }
}

public class a11_MapManyToMany {
    public static void main(String[] args) {
        //根据结构设置对象数据
        Member memA=new Member("mldn", "张三");
        Member memB=new Member("mldnjava", "李四");
        Product proA=new Product(1,"Java开发图书",89.8);
        Product proB=new Product(2,"耳机",2309.8);
        Product proC=new Product(3,"小米手机",3000.8);
        memA.setProducts(new Product[]{proA,proB,proC});
        memB.setProducts(new Product[]{proA});
        proA.setMembers(new Member[]{memA,memB});
        proB.setMembers(new Member[]{memA});
        proC.setMembers(new Member[]{memA});
        //根据要求获取数据
        System.out.println("----------根据用户查看浏览商品信息---------");
        System.out.println(memA.getInfo());
        for (int i = 0; i < memA.getProducts().length; i++) {
            System.out.println("\t|-"+memA.getProducts()[i].getInfo());
        }
        System.out.println("----------根据商品找到被浏览的记录---------");
        System.out.println(proA.getInfo());
        for (int i = 0; i < proA.getMembers().length; i++) {
            System.out.println("\t|-"+proA.getMembers()[i].getInfo());
        }
    }
}
